package dhy.lyb.change.controller;

import dhy.lyb.change.enums.MessageEnum;
import dhy.lyb.change.model.*;
import dhy.lyb.change.service.UserService;
import dhy.lyb.change.service.UserThreeService;
import dhy.lyb.change.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Map;

/**
 * 用户查找相关控制器
 * 简单restful接口，不添加后缀
 * Created by Administrator on 2016/11/21.
 */
@Controller
@Scope("prototype")
public class UserInfoController extends BaseController {

    @Autowired
    @Qualifier("userService")
    private UserService userService;

    @Autowired
    private UserThreeService userThreeService;

    /**
     * 依据 具体条件 获取记录
     *
     * @return 用户详细信息
     */
    @RequestMapping(value = "user/getUserByConditions")
    @ResponseBody
    public User getUserByConditions(@RequestBody User user) {
        System.out.println(user);
        if (user == null) {
            return null;
        }
        System.out.println(user.toString());
        List<User> users = userService.getRecordByCondition(user);
        if (users.size() == 0) {
            System.out.println(users);
            return null;
        }
        return users.get(0);
    }

    /**
     * 用户登录接口
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "user/userLogin")
    @ResponseBody
    public ExtendModel userLogin(@RequestBody User user) {
        System.out.println(user);
        if (user == null) {
            return null;
        }
        System.out.println(user.toString());
        int value = userService.checkLogin(user);
        if (value != MessageEnum.OPERATE_ERROR.getValue()) {
            return new ExtendModel("100", MessageEnum.ADMIN_INFO_SUCCESS.getDescription(), value);
        }
        return new ExtendModel("111", MessageEnum.ADMIN_INFO_ERROR.getDescription(), value);
    }

    @RequestMapping(value = "user/getUserActivityItemById")
    @ResponseBody
    public UserActivityCustom getUserActivityItemById(@RequestBody User user) {
        System.out.println(user);
        if (user == null) {
            return null;
        }
        System.out.println(user.toString());
        return userService.getUserActivityItemById(user);
    }

    /**
     * 用户列表（训练项目信息）
     *
     * @return
     */
    @RequestMapping(value = "user/getUserActivity")
    @ResponseBody
    public List<User> getUserActivity() {
        List<User> users = userService.getUserActivityList();
        if (users == null) {
            return null;
        }
        System.out.println(users.toString());
        return users;
    }

    @RequestMapping(value = "user/getUserActivities")
    public void getUserActivities() {
    }

    @RequestMapping(value = "user/activity")
        public void geUserActivity() {
    }

    @RequestMapping(value = "user/checckMobile")
    @ResponseBody
    public ExtendModel checckMobile(@RequestBody String jsonCode){
        if(jsonCode == null){
            return super.errorResult();
        }

        Map<String, String> maps = JsonUtil.changeGsonToMaps(jsonCode);

        User user = new User();
        user.setMobile((String) maps.get("mobile"));

        int value = userService.checkMobile(user);
        System.out.println(value);
        if (value == 0) {
            return super.successResult(0);
        } else {
            return super.insResult(value, user.getId());
        }
    }

    @RequestMapping(value = "user/getUserThreeList")
    @ResponseBody
    public List<UserThree> getUserThreeList(@RequestBody String jsonCode){
        Map<String, String> maps = JsonUtil.changeGsonToMaps(jsonCode);
        UserThree userThree = new UserThree();
        userThree.setId(Integer.parseInt(maps.get("id")));
        return userThreeService.getRecordByCondition(userThree);
    }

    /**
     * 接口测试
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "user/getUser")
    @ResponseBody
    public User getUser(@RequestBody User user) {
        return user;
    }
}
